import {createRouter, createWebHashHistory} from 'vue-router'
import * as nProgress from 'nprogress'
import 'nprogress/nprogress.css'

nProgress.configure({
  showSpinner: false
})

const Test = () => import('@/pages/test')
const Print = () => import('@/pages/print')
const Layout = () => import('@/components/AppLayout')
const EnterStore = () => import('@/pages/enterStore') // 入库
const OutStore = () => import('@/pages/outStore') // 出库
const PrivateStore = () => import('@/pages/privateStore') // 私人云仓
const Login = () => import('@/pages/loginPage') // 登录

const routes = [
  {
    path: '/',
    name: '',
    component: Layout,
    children: [
      {
        path: '/',
        name: 'EnterStore',
        component: EnterStore,
        meta: {
          title: '入库'
        }
      },
      {
        path: '/outStore',
        name: 'OutStore',
        component: OutStore,
        meta: {
          title: '出库'
        }
      },
      {
        path: '/privateStore',
        name: '/PrivateStore',
        component: PrivateStore,
        meta: {
          title: '私人云仓'
        }
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    meta: {
      title: '登录'
    },
    component: Login
  },
  {
    path: '/test',
    name: 'Test',
    meta: {
      title: '测试页面'
    },
    component: Test
  },
  {
    path: '/print',
    name: 'Print',
    meta: {
      title: '打印界面'
    },
    component: Print
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes,
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 })
})


// 全局前置守卫
router.beforeEach((to, from) => {
  document.title = import.meta.env.VITE_NAME + (to.meta.title || import.meta.env.VITE_TITLE)
  nProgress.start()
  return true
})

// 全局后置钩子
router.afterEach(() => {
  nProgress.done(true)
})

export default router
